#include "diode.h"

diode::diode()
{

}

void diode::setV_TH(float V_TH)
{
    this->V_TH = V_TH;
}

float diode::getV_TH()
{
    return this->V_TH;
}

void diode::setV_ON(float V_ON)
{
    this->V_ON = V_ON;
}

float diode::getV_ON()
{
    return this->V_ON;
}

void diode::setV_BR(float V_BR)
{
    this->V_BR = V_BR;
}

float diode::getV_BR()
{
    return this->V_BR;
}

void diode::setV_D(float V_D)
{
    this->V_D = V_D;
}

float diode::getV_D()
{
    return this->V_D;
}

void diode::setV_input(float V_input)
{
    this->V_input = V_input;
}

float diode::getV_input()
{
    return this->V_input;
}

void diode::setV_output(float V_output)
{
    this->V_output = V_output;
}

float diode::getV_output()
{
    return this->V_output;
}

void diode::setI_S(float I_S)
{
    this->I_S = I_S;
}

float diode::getI_S()
{
    return this->I_S;
}

void diode::setI_D(float I_D)
{
    this->I_D = I_D;
}

float diode::getI_D()
{
    return this->I_D;
}

float diode::calI_D(float I_S, float V_D)
{
    float I_D = I_S * (qPow(2.718281828459,(V_D/0.026)) - 1);
    return I_D;
}


void diode::init_diode(float V_ON, float V_TH, float V_BR, float I_S)
{
    QwtPlot *p = new QwtPlot();


    p->setTitle("二极管伏安特性曲线");
    p->setAxisTitle(QwtPlot::xBottom, "二极管两端电压");
    p->setAxisTitle(QwtPlot::yLeft, "流过二极管的电流");
    //p->setParent(ui->centralwidget);
    p->resize(800, 500);
    p->show();
    QwtPlotGrid *grid = new QwtPlotGrid;
    grid->enableX( true );//设置网格线
    grid->enableY( true );
    grid->setMajorPen( Qt::black, 0, Qt::DotLine );
    grid->attach( p );

    QwtPlotCurve *curve = new QwtPlotCurve("curve");

    curve->setPen(Qt::blue,2);//设置曲线颜色 粗细
    curve->setRenderHint(QwtPlotItem::RenderAntialiased,true);//线条光滑化

    //QwtSymbol *symbol = new QwtSymbol( QwtSymbol::Ellipse,
    //QBrush( Qt::yellow ), QPen( Qt::red, 2 ), QSize( 6, 6) );//设置样本点的颜色、大小
    //curve->setSymbol( symbol );//添加样本点形状

    QPolygonF points1;//输入节点数据QPointF(x,y)



    this->setV_ON(V_ON);
    this->setV_TH(V_TH);
    this->setV_BR(V_BR);
    this->setI_S(I_S);
    for(float V_D = -160.0; V_D <= 0; V_D += 5){    //分段函数

        if(V_D <= this->getV_BR()){
            this->setI_D(-2);
            //points1<<QPointF(d.getV_D(),d.getI_D());
        }else if (V_D > this->getV_BR()){
            this->setI_D(this->getI_S());
            //points1<<QPointF(d.getV_D(),d.getI_D());
        }
        qDebug()<<this->getI_D();
    }
    for(float V_D = 0.0; V_D <= 0.8; V_D += 0.01){
        this->setV_D(V_D);
        this->setI_D(this->calI_D(this->getI_S(), this->getV_D()));
        points1<<QPointF(this->getV_D(),this->getI_D());
        qDebug()<<this->getI_D();
    }
    curve->setSamples(points1);
    curve->attach( p );
}
